﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Agile.Engine.Service
{
    /// <summary>
    /// 默认服务调用处理
    /// </summary>
    public class DefaultServiceProcess : ServiceProcess
    {
        public Hashtable Param { get; set; }
        public Hashtable Result { get; set; }

        public override object InitializeParam(object model)
        {
            Hashtable m = new Hashtable(model as Hashtable);
            m["__dataContext"] = Context;
            m["__dataProvider"] = Provider;
            m["__dataRule"] = Rule;
            return m;
        }

        public override string EncodeParam(object param)
        {
            param = param as Hashtable;
            return Agile.Engine.Utils.StringUtils.ToJsonBase64(param);
        }

        public override object DecodeResult(string param)
        {
            Hashtable model = Agile.Engine.Utils.StringUtils.FromJsonBase64<Hashtable>(param);
            Status = Convert.ToInt32(model["__status"]);
            ///取指定返回信息
            //if (Status == 0)
            //    Message = Agile.Engine.Utils.StringUtils.IsEmpty(model["__msg"]) ? string.Empty : model["__msg"].ToString();
            //else if (Status < 1)
            //{
            //    ///取消息信息
            //}
            Message = model["__msg"].ToString();
            return model;
        }

        public override void InvokeCallback(IAsyncResult ar)
        {

        }

        public override bool ComplateInvoke(object param, object result)
        {
            bool success = Status >= 0;
            if (ComplateCallback != null)
            {
                ComplateCallback(success, result);
            }
            Result = result as Hashtable;
            return success;
        }
    }
}
